/ _ \ \_\(_)/_/ _//"\\_ more on JOHLEM.net / \ 0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0 ============================ REGULAR EXPRESSIONS ============================ **OVERVIEW** Regex are patterns for string matching, searching, and replacing. **ANCHORS** - `^` : Start of string (e.g., `^abc`) - `$` : End of string (e.g., `xyz$`) **QUANTIFIERS** - `*` : 0 or more (e.g., `ab*`) - `+` : 1 or more (e.g., `ab+`) - `?` : 0 or 1 (e.g., `ab?`) - `{n}` : Exactly n times (e.g., `a{3}`) - `{n,}` : At least n times (e.g., `a{2,}`) - `{n,m}` : Between n and m times (e.g., `a{2,4}`) **CHARACTER CLASSES** - `.` : Any character except newline - `[abc]` : Any character in set - `[^abc]` : Any character NOT in set - `[a-z]` : Range (e.g., `[0-9]` for digits) **SPECIAL SEQUENCES** - `\d` : Digit (0-9) - `\D` : Non-digit - `\w` : Word character (letters, digits, `_`) - `\W` : Non-word character - `\s` : Whitespace (space, tab, newline) - `\S` : Non-whitespace **GROUPS & REFERENCES** - `(abc)` : Capturing group - `(?:abc)` : Non-capturing group - `\1` : Backreference (e.g., `(a)\1` matches "aa") **ALTERNATION** - `|` : Logical OR (e.g., `cat|dog`) **FLAGS** - `i` : Case-insensitive - `g` : Global match - `m` : Multiline mode - `s` : Dot matches newline **ESCAPING** Escape special characters like `. * + ? ^ $ { } [ ] \ | ( )` with `\`. Example: Match `1+1=2` as `1\+1=2`. ---------------------------- COMMON USE CASES/EXAMPLES ---------------------------- - **Email Validation**: `/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i` - **Phone Number Validation**: `/^\+?[1-9]\d{1,14}$/` - **Extract All Digits**: `/\d+/g` - **Find Words**: `/\b\w+\b/g` - **Remove HTML Tags**: `/<[^>]*>/g` - **Password Strength Check** (At least 8 characters, one letter, one digit): `/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/` **TIPS** - Test regex with [regex101](https://regex101.com). - Use flags/modifiers to simplify patterns. - Comment complex regex with `(?x)` for clarity. **TABLE OF COMMON FEATURES** | Feature | Symbol | Example | |----------------|---------|-----------------------| | Any character | `.` | `a.b` → matches "acb" | | Start of text | `^` | `^abc` → "abc" start | | End of text | `$` | `xyz$` → "xyz" end | | Optional match | `?` | `colou?r` → "color" | | Word boundary | `\b` | `\bword\b` → "word" | **CONCLUSION** Regex is a versatile tool for text processing. Master it to boost efficiency in programming, data analysis, and more. Practice regularly to sharpen your skills!